package ui.panel;

import cerebrum.cache.ImageLogStatusCache;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ui.factory.SingletonFactory;
import ui.panel.setup.WallpaperTypeChoiceStage;

public class DownloadLogStage {
    private Logger logger = LoggerFactory.getLogger(DownloadLogStage.class);
    private static DownloadLogStage instance = null;
    //记录选中的哪一个按钮 true 实时 or false 历史
    private Boolean buttonStatus = true;
    //调用单例工厂
    public static DownloadLogStage getInstance() {
        if (instance == null) {
            instance = SingletonFactory.getWeakInstace(DownloadLogStage.class);
        }
        return instance;
    }

    Pane pane = new Pane();
    TextArea log = new TextArea();
    Process p;

    public Pane getDoTextAreaLogPane() {
        pane.setPrefWidth(555);
        pane.setPrefHeight(260);
        pane.setStyle("-fx-background-color:#f07c82;");
        pane.setLayoutY(200);
        pane.setLayoutX(500);
        pane.setId("logPane");

        log.setPrefWidth(450);
        log.setLayoutX(50);
        log.setLayoutY(40);
        log.setWrapText(true);
        Button min = minButton();
        Button close = closeButton();

        ImageView logFrame = new ImageView(new Image("/fxml/css/images/log/log_frame.png"));
        logFrame.setFitHeight(470);
        logFrame.setFitWidth(830);
        logFrame.setLayoutX(-100);
        logFrame.setLayoutY(-120);
        logFrame.setOpacity(0.9);


        pane.getChildren().addAll(min, close, logFrame, log);
        log.setText("");
        //设置不在刷新实时日志
        ImageLogStatusCache.setLog4j2LogStatus(true);
        new Thread(() -> {
            //后台程序开始运行
            logtest();
        }).start();
        return pane;
    }


    private void logtest() {
        Long i = 1L;
        while (1 == 1) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {

            }
            logger.info(">>>>>>>>>>  " + i++);

        }
    }

    private Button minButton() {
        Button min = new Button();
        min.setLayoutX(500);
        min.setLayoutY(5);
        min.setId("min");
        min.setStyle(buttonCss("/mini1.png", 28));

        //点击
        min.setOnMouseClicked(event -> {
            WallpaperTypeChoiceStage instance = WallpaperTypeChoiceStage.getInstance();
            instance.removeNode((Pane) instance.getPane(), "logPane");
            pane.getChildren().clear();
        });
        //鼠标退出
        min.setOnMouseExited(event ->
                min.setStyle(buttonCss("/mini1.png", 28))
        );
        //鼠标经过
        min.setOnMouseEntered(event ->
                min.setStyle(pressedCss("mini3.png", 28))
        );

        min.setTooltip(new Tooltip("最小化"));
        //设置提示延迟出现时间
        min.getTooltip().setShowDelay(Duration.millis(200));

        return min;
    }

    private Button closeButton() {
        Button close = new Button();
        close.setLayoutX(530);
        close.setLayoutY(5);
        close.setStyle(buttonCss("close1.png", 13));

        //点击
        close.setOnMouseClicked(event -> {
            WallpaperTypeChoiceStage instance = WallpaperTypeChoiceStage.getInstance();
            instance.removeNode((Pane) instance.getPane(), "logPane");
            pane.getChildren().clear();
        });
        close.setOnMouseExited(event ->
                close.setStyle(buttonCss("/close1.png", 13))
        );
        close.setOnMouseEntered(event ->
                close.setStyle(pressedCss("close2.png", 28))
        );

        close.setTooltip(new Tooltip("关闭"));
        //设置提示延迟出现时间
        close.getTooltip().setShowDelay(Duration.millis(200));

        return close;
    }

    private String buttonCss(String imageURL, int buttonSize) {
        return "-fx-background-image:url('/fxml/css/images/" + imageURL + "');" +
                "    -fx-background-size:" + buttonSize + "px " + buttonSize + "px;" +
                "    -fx-background-repeat: no-repeat;" +
                "    -fx-background-position:center;" +
                "    -fx-background-color:transparent;";
    }

    private String pressedCss(String imageURL, double buttonSize) {
        return " -fx-background-image:url('/fxml/css/images/" + imageURL + "');" +
                " -fx-background-size:" + buttonSize + "px " + buttonSize + "px;" +
                "-fx-background-repeat: no-repeat;" +
                "-fx-background-position:center;" +
                "-fx-background-color: transparent;" +
                "-fx-focused:hand;";
    }

    public void log(final String st) {
        // we can access fx objects only from fx thread
        // so we need to wrap log access into Platform#runLater
//        Platform.runLater(() -> {
        log.appendText(st + "\n");
        //设置鼠标指针位置
        log.positionCaret(log.getText().length());
//        });
    }


}
